//
//  LSZStatusPictureView.swift
//  醒浪微博
//
//  Created by 雷顺哲 on 16/7/15.
//  Copyright © 2016年 LSZ. All rights reserved.
//

import UIKit

//重用标记
private let PictureViewCellIdentifier = "PictureViewCellIdentifier"
//每项的间距
private let itemMargin: CGFloat = 5
//每项的宽度
private let itemWidth: CGFloat = (ScreenWidth - 2 * HomeCellMargin - 2 * itemMargin) / 3
//配图视图
class LSZStatusPictureView: UICollectionView {
//        配图的数据源
    var picUrls: [LSZStatusPictureInfo]? {
        didSet{
            messageLabel.text = "\(picUrls?.count ?? 0)"
            let size = calcPictureViewSize(picUrls?.count ?? 0)
            
//            设置配图的指定大小
            self.snp_makeConstraints { (make) -> Void in
                make.size.equalTo(size)
            }
//            刷新数据
            self.reloadData()
        }
    }
    
    // MARK:懒加载控件
    private lazy var messageLabel: UILabel = {
       let label = UILabel(textColor: UIColor.yellowColor(), fontSize: 30)
        return label
    }()
    
    override init(frame: CGRect, collectionViewLayout layout: UICollectionViewLayout) {
        let flowLayout = UICollectionViewFlowLayout()
//        设置大小
        flowLayout.itemSize = CGSize(width: itemWidth, height: itemWidth)
//        设置水平间距
        flowLayout.minimumInteritemSpacing = itemMargin
//        垂直间距
        flowLayout.minimumLineSpacing = itemMargin
        
        super.init(frame: frame, collectionViewLayout: flowLayout)
        setUpUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setUpUI() {
//        注册cell
        registerClass(CZStatusPictureViewCell.self, forCellWithReuseIdentifier: PictureViewCellIdentifier)
//        设置数据源
        dataSource = self
        addSubview(messageLabel)
        
        messageLabel.snp_makeConstraints { (make) -> Void in
            make.center.equalTo(self)
        }
    }

//    根据图片个数计算配图大小
    private func calcPictureViewSize(count: Int) -> CGSize {
//        列数
        let cols = count > 3 ? 3 : count
//        行数
        let rows = (count - 1) / 3 + 1
//        计算当前配图的宽度
        let width = CGFloat(cols) * itemWidth + CGFloat(cols - 1) * itemMargin
//        计算当前配图的高度
        let height = CGFloat(rows) * itemWidth + CGFloat(rows - 1) * itemMargin
        
        return CGSize(width: width, height: height)
    }
  
}



// UICollectionViewDataSource
extension LSZStatusPictureView: UICollectionViewDataSource{
    
    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return picUrls?.count ?? 0
    }
    
    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(PictureViewCellIdentifier, forIndexPath: indexPath) as! CZStatusPictureViewCell
        cell.picInfo = picUrls![indexPath.item]
        return cell
    }
}

//自定义配图cell
class CZStatusPictureViewCell: UICollectionViewCell {
//    设置模型
    var picInfo: LSZStatusPictureInfo? {
        didSet{
            
            if let imageUrl = picInfo?.thumbnail_pic {
                
                imageView.sd_setImageWithURL(NSURL(string: imageUrl),placeholderImage: UIImage(named: "timeline_image_placeholder"))
                
//                判断图片是否是gif图
//                if imageUrl.hasSuffix(".gif") {
//                    gifImageView.hidden = false
//                } else {
//                    gifImageView.hidden = true
//                }
                gifImageView.hidden = !imageUrl.hasSuffix(".gif")
                
                
            }
            
        }
    }
    
    // MARK:懒加载控件
    
    private lazy var imageView: UIImageView = {
       let view = UIImageView(image: UIImage(named: "timeline_image_placeholder"))
//        保证图片原始比例
        view.contentMode = UIViewContentMode.ScaleAspectFit
//        剪切超出的多余图片
        view.clipsToBounds = true
        
        return view
    }()
    
//    gif图片
    private lazy var gifImageView: UIImageView = UIImageView(image: UIImage(named: "timeline_image_gif"))
    override init(frame: CGRect) {
        super.init(frame: frame)
        setUpUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
//    添加控件设置约束
    private func setUpUI() {
        contentView.addSubview(imageView)
        contentView.addSubview(gifImageView)
        
        imageView.snp_makeConstraints { (make) -> Void in
            make.edges.equalTo(contentView).offset(UIEdgeInsetsZero)
        }
        
        gifImageView.snp_makeConstraints { (make) -> Void in
            make.trailing.equalTo(imageView)
            make.bottom.equalTo(imageView)
        }
    }
    
    
}












